package com.gcb.socket;

/**
 * Created by IntelliJ IDEA.
 * User: chendt
 * Date: 2008-7-25
 * Time: 10:55:59
 * To change this template use File | Settings | File Templates.
 */
import java.io.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import javax.swing.*;
import java.io.FileOutputStream;
import java.net.*;


public class form extends Applet implements ActionListener
{
  private Frame f;
  private Button enter;
  private Button cancle;
  private Button exit;
  private Button Entname;
  private Button connect;

  private TextField text,usename;
  File file;
  private TextArea ta;
  public FileOutputStream dout=null;
  public FileInputStream  din =null;
  byte[] buf = new byte[1024];
  PrintWriter pout;
  BufferedReader in;
  Socket socket;
  StringBuffer sb=new StringBuffer();
  String name="";   //用于定义用户名

  public static void main(String args[])
  {
    form myform=new form();
    myform.go();

  }
  public void go()
  {
    f=new Frame("客户端");
    f.setLayout(new FlowLayout());
    enter=new Button("确定");
    cancle=new Button("取消");
    exit=new Button("退出");
    connect=new Button("连接");
    text=new TextField("在这里输入消息",70);
    ta=new TextArea(15,70);
    usename=new TextField("输入用户名",30);
    Entname=new Button("使用名字");


    ta.setEditable(false);
    f.add(text);
    f.add(enter);
    f.add(cancle);
    f.add(exit);
    f.add(connect);
    f.add(ta);
    f.add(usename);
    f.add(Entname);

    f.setSize(600,400);
    f.setVisible(true);
    file=new File(".","Sent.txt");
    ta.setText(readfile());
    enter.addActionListener(this);
    cancle.addActionListener(this);
    exit.addActionListener(this);
    connect.addActionListener(this);
    Entname.addActionListener(this);

  }
  public void actionPerformed(ActionEvent e)
  {

     String str="";
     if(e.getSource()==connect)
     {
       if(connect.getLabel().equals("连接"))
       {
         if(name.equals(""))
         {
            JOptionPane.showMessageDialog(null,   "请先输入用户名");
         }
         else
         {
           try
           {
             socket=new Socket("10.17.178.165",2000); //开始连接服务器端
             pout=new PrintWriter(socket.getOutputStream(),true);
             pout.println(name);

             connect.setLabel("断开");
           } catch(IOException ioe){
             JOptionPane.showMessageDialog(null,   "连接不成功");
           }

         }
       }
       else
       {
         if(socket.isConnected())
         {
           try
           {
              socket.close();
           }catch(IOException ioe){}
           connect.setLabel("连接");
         }
         else
         {
            JOptionPane.showMessageDialog(null,   "已断开!");
         }

       }
     }
     if(e.getSource()==enter)
     {
       if(text.getText().equals(""))
       {
         //提示不要发送空消息
         JOptionPane.showMessageDialog(null,   "还没输入东西呢!");
       }
       else
       {
         if(name.equals(""))
         {
              JOptionPane.showMessageDialog(null,   "请先输入用户名!");
         }
         else
         {
            SimpleDateFormat m=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            str=m.format(new Date())+" "+name+" "+text.getText();
            System.out.println(str);
            ta.append(str+"\n");
            try
            {
              //写入文件
              ///////////////////////////////////////////////////////////////
              dout = new FileOutputStream(file,true);    //true表示每次从文件尾写消息
              str=str+"\r\n";  //window换行符
              dout.write(str.getBytes());
              ///////////////////////////////////////////////////////////////
              //发送消息
              ///////////////////////////////////////////////////////////////
              pout.println(text.getText());
              JOptionPane.showMessageDialog(null,   "发送成功");
              ///////////////////////////////////////////////////////////////
            }catch(IOException ioe){ }
            
            text.setText("");
         }
       }
     }
    if(e.getSource()==cancle)
    {
      text.setText("");
    }
    if(e.getSource()==exit)
    {
      System.exit(0);
    }
    if(e.getSource()==Entname&&(!usename.getText().equals("")))
    {
        name=usename.getText();
        usename.setVisible(false);
        Entname.setVisible(false);
    }

  }
  public String readfile()
    {
      ///////////////////////////////////////////////
      try
      {
          din=new FileInputStream(file);
          while((din.read(buf))!=-1)
          {
              sb.append(new String(buf));
              buf=new byte[1024];//重新生成，避免和上次读取的数据重复
          }
      }catch(IOException ioe){}
      ///////////////////////////////////////////////
      return sb.toString();
    }
   
}


